<?php

namespace app\controller;

use app\BaseController;
use app\message;
use app\model\CluePerson;
use app\model\SystemOperateLog;
use app\model\UnitInfo;
use app\Request;
use app\service\Certificate;
use app\service\Clue;
use app\service\CluePersonServer;
use app\service\CustomerPerson;
use app\service\Power;
use app\service\RedisService;
use app\service\System;
use app\stateData;
use app\validate\CustomerEnterPriseList;
use app\validate\CustomerPersonList;
use think\Db;

class CustomManage extends BaseController
{
    /**
     * 客户管理
     */

    //人才客户
    public function getCustomPersonList(
        Request            $request,
        CluePerson         $person,
        Clue               $clue,
        Power              $power,
        \app\service\Order $orderSer,
        CustomerPerson  $customerPerson,
        RedisService $redisService
    ){
        $aliasId = $request->get('aliasId');
        $uid = $request->get('user_login_id');
        $param = $request->get();
        $page = $request->get('page') ?: 1;
        $pageNum = $request->get('pageNum') ?: stateData::$_page;

        if (empty($aliasId)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $validate = new CustomerPersonList();
        if (!$validate->check($param)) {
            return ReJson(message::$ERROR_CODE, $validate->getError(), []);
        }
        $flag = flagStatus($param);
        $ids = $customerPerson->spliceWhere($param);
        $ids = !$ids ? [0] : $ids;

        //根据条件查询用户范围
        $userData = $power->getUserDataRange($uid);

        $uid_ids = $person->getPersonIds($userData);

        $ids = handlePersonIds($uid_ids, $ids, $flag);
        $fieldListTree = $power->getUserFieldPower($aliasId, $uid);

        //根据uid集合的数据
        $dataId = $person->getPersonList($userData, $page, $pageNum, $ids);

        $data = $redisService->getPersonDataReSet ($dataId,$uid);

        //删除订单为0的数据
        $result = $orderSer->delOrderEmpty($data);

        $pages['pageNumber'] = $pageNum;
        $pages['total'] = count($result);
        return ReJson(
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            [   //删除没有成交过的订单
                'data' => page($result,$page,$pageNum,'desc','create_date',false),
                'power' => $fieldListTree
            ],
            $pages
        );
    }

    //企业客户
    public function getCustomEnterPriseList(
        \app\model\ClueEnterprise           $enterprise,
        Request                             $request,
        Certificate                         $certificate,
        Power                               $power,
        \app\service\Order                  $orderSer,
        \app\service\CustomerEnterPriseList $customerEnterPriseList,
        RedisService $redisService
    ){
        $aliasId = $request->get('aliasId');
        $page = $request->get('page') ?: 1;
        $pageNum = $request->get('pageNum') ?: stateData::$_page;
        $uid = $request->get('user_login_id');

        $param = $request->get();
        $validate = new CustomerEnterPriseList();
        if (!$validate->check($param)) {
            return ReJson(message::$ERROR_CODE, $validate->getError(), []);
        }

        //判断参数是否有值
        $flag = false;
        $key_arr = [
            'aliasId',
            'page',
            'pageNum',
            'user_login_id',
        ];
        foreach ($param as $key => $item) {
            if (in_array($key, $key_arr)) {
                continue;
            }
            if ($item) {
                $flag = true;
                break;
            }
        }
        //符合条件的数据
        $ids = $customerEnterPriseList->spliceWhere($param);
        $ids = !$ids ? [0] : $ids;

        $fieldListTree = $power->getUserFieldPower($aliasId, $uid);

        //根据条件查询用户范围
        $userData = $power->getUserDataRange($uid);

        //根据用户范围和和符合条件的数据进行合并
        $uid_ids = $enterprise->getEnterIds($userData);
        $ids = handlePersonIds($uid_ids, $ids, $flag);

        //查询权限所有数据
        $dataId = $enterprise->getEnterList($userData, $page, $pageNum, $ids);

        $data = $redisService->getEnterData ($dataId,$uid);

        //删除订单为0的数据
        $result = $orderSer->delOrderEmpty($orderSer->getEnterOrderCountNum($data, $uid));

        $pages['pageNumber'] = $pageNum;
//        $pages['total'] = $enterprise->getEnterCount($ids);
        $pages['total'] = count($result);
        return ReJson(
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            [
                'data' => page($result,$page,$pageNum),
                'power' => $fieldListTree
            ],
            $pages
        );
    }

    //其他企业  1出证单位  2聘用单位  3注册单位
    public function getCustomOtherList(
        UnitInfo                  $unit,
        Request                   $request,
        Power                     $power,
        \app\service\CustomManage $customManage,
        \app\service\Order $orderSer
    ){
        $aliasId = $request->get('aliasId');

        $type = $request->get('type') ?: 1;
        $page = $request->get('page') ?: 1;
        $pageNum = $request->get('pageNum') ?: stateData::$_page;
        $uid = $request->get('user_login_id');

        $param = $request->get();
        $validate = new \app\validate\UnitInfo();
        if (!$validate->check($param)) {
            return ReJson(message::$ERROR_CODE, $validate->getError(), []);
        }

        $ids = $customManage->spliceWhere($param); //权限

        $fieldListTree = $power->getUserFieldPower($aliasId,$uid);
        //拼接查询订单量
        $data = $orderSer->getCzdwOrderNum($unit->getTypeListModel($type, $page, $pageNum, $ids),$uid);

        $result = $orderSer->delOrderEmpty($data);

        $pages['pageNumber'] = $pageNum;
        $pages['total'] =  count($result);

        return ReJson(
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            [
                'data' => page($result,$page,$pageNum),
                'power' => $fieldListTree
            ],
            $pages
        );
    }

    //获取出证|聘用|注册单位
    public function getUnitDetails(Request $request,UnitInfo $info)
    {
        $id = $request->get('id');

        if (empty($id)) return ReJson(message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

        $result =   \think\facade\Db::table('unit_info')->where('id',$id)->find();
        if(!$result){
            $result = [];
        }else{
            if($result['secure_expire_date'] == '0000-00-00 00:00:00'){
                $result['secure_expire_date'] = '';
            }
        }
//        $result = $info->getIdDataModel($id);

        return ReJson(message::$SUCC_CODE,message::$SUCC_SELECT_DATA,$result);
    }

    //删除其他企业
    public function delCustomOther(
        Request          $request,
        UnitInfo         $unit,
        SystemOperateLog $systemOperateLogModel
    ){
        $id = $request->post('id');
        $uid = $request->post ('user_login_id');

        if (empty($id)) ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        try {
            $data = explode(',', $id);

            if (is_array($data)) foreach ($data as $datum) {
                $unit->delIdModel($datum);
            }
            $systemOperateLogModel->addOperate ($uid,message::$KHGL,'客户管理删除企业'.$id);
        } catch (\Exception $e) {
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_DATELE_DATA);
    }

    //编辑其他企业
    public function editCustomOther(
        Request $request,
        UnitInfo $unit,
        System $system,
        SystemOperateLog $systemOperateLogModel
    ){
        $param = $request->post();
        $uid = $request->post ('user_login_id');
        if(!$param['area'])
        {
            $param['province'] = $param['city'] = '';
        }else{
            $area_arr = $system->getSuperAreaArray($param['area']);
            $param['province'] = $area_arr[0];
            $param['city'] = $area_arr[1];
        }
        if (empty($param['id'])) ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $unit->editTypeDataModel($param);
        $systemOperateLogModel->addOperate ($uid,message::$KHGL,'客户管理编辑企业'.$param['id']);

        return ReJson(message::$SUCC_CODE, message::$SUCC_UPDATE);
    }

}